Imports System.ComponentModel
Imports System.IO


Imports Phoenix.DataAccess
Imports Phoenix.Forms.Controls
Imports Phoenix.Preferences


Namespace Forms

    Public Class BillNoSaleForm : Inherits ChildFormBase

#Region " Windows Form Designer generated code "

        Public Sub New()
            MyBase.New()

            'This call is required by the Windows Form Designer.
            InitializeComponent()

            'Add any initialization after the InitializeComponent() call
            SaveBarItem.Enabled = False
            SaveBarItem.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        End Sub

        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents lblSum As System.Windows.Forms.Label
        Friend WithEvents Label1 As System.Windows.Forms.Label
        Friend WithEvents DescrMemo As DevExpress.XtraEditors.MemoEdit
        Friend WithEvents Belevel1 As System.Windows.Forms.GroupBox
        Friend WithEvents EmployeeControl As Phoenix.Forms.Controls.EmployeeFieldEdit
        Friend WithEvents ErrorProvider As System.Windows.Forms.ErrorProvider
        Friend WithEvents SumBox As DevExpress.XtraEditors.CalcEdit
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(BillNoSaleForm))
            Me.DescrMemo = New DevExpress.XtraEditors.MemoEdit
            Me.lblSum = New System.Windows.Forms.Label
            Me.EmployeeControl = New Phoenix.Forms.Controls.EmployeeFieldEdit
            Me.Label1 = New System.Windows.Forms.Label
            Me.Belevel1 = New System.Windows.Forms.GroupBox
            Me.ErrorProvider = New System.Windows.Forms.ErrorProvider
            Me.SumBox = New DevExpress.XtraEditors.CalcEdit
            CType(Me.DescrMemo.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.SumBox.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            Me.SuspendLayout()
            '
            'DescrMemo
            '
            Me.DescrMemo.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                        Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.DescrMemo.EditValue = ""
            Me.DescrMemo.Location = New System.Drawing.Point(8, 128)
            Me.DescrMemo.Name = "DescrMemo"
            Me.DescrMemo.Size = New System.Drawing.Size(512, 270)
            Me.DescrMemo.TabIndex = 2
            '
            'lblSum
            '
            Me.lblSum.Location = New System.Drawing.Point(8, 66)
            Me.lblSum.Name = "lblSum"
            Me.lblSum.Size = New System.Drawing.Size(80, 16)
            Me.lblSum.TabIndex = 3
            Me.lblSum.Text = "&Amount:"
            '
            'EmployeeControl
            '
            Me.EmployeeControl.EditValue = Nothing
            Me.EmployeeControl.Location = New System.Drawing.Point(96, 88)
            Me.EmployeeControl.Name = "EmployeeControl"
            Me.EmployeeControl.Size = New System.Drawing.Size(304, 20)
            Me.EmployeeControl.TabIndex = 1
            '
            'Label1
            '
            Me.Label1.Location = New System.Drawing.Point(8, 90)
            Me.Label1.Name = "Label1"
            Me.Label1.Size = New System.Drawing.Size(80, 16)
            Me.Label1.TabIndex = 0
            Me.Label1.Text = "&Staff Member:"
            '
            'Belevel1
            '
            Me.Belevel1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.Belevel1.Location = New System.Drawing.Point(8, 116)
            Me.Belevel1.Name = "Belevel1"
            Me.Belevel1.Size = New System.Drawing.Size(512, 3)
            Me.Belevel1.TabIndex = 33
            Me.Belevel1.TabStop = False
            '
            'ErrorProvider
            '
            Me.ErrorProvider.ContainerControl = Me
            '
            'SumBox
            '
            Me.SumBox.Location = New System.Drawing.Point(96, 64)
            Me.SumBox.Name = "SumBox"
            '
            'SumBox.Properties
            '
            Me.SumBox.Properties.Buttons.AddRange(New DevExpress.XtraEditors.Controls.EditorButton() {New DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)})
            Me.SumBox.Properties.DisplayFormat.FormatString = "c2"
            Me.SumBox.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
            Me.SumBox.Properties.EditFormat.FormatString = "f2"
            Me.SumBox.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.Numeric
            Me.SumBox.Properties.Precision = 2
            Me.SumBox.Size = New System.Drawing.Size(120, 20)
            Me.SumBox.TabIndex = 4
            '
            'BillNoSaleForm
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(528, 406)
            Me.Controls.Add(Me.SumBox)
            Me.Controls.Add(Me.Belevel1)
            Me.Controls.Add(Me.Label1)
            Me.Controls.Add(Me.EmployeeControl)
            Me.Controls.Add(Me.lblSum)
            Me.Controls.Add(Me.DescrMemo)
            Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
            Me.Name = "BillNoSaleForm"
            Me.Text = "No Sale Bill"
            Me.Controls.SetChildIndex(Me.DescrMemo, 0)
            Me.Controls.SetChildIndex(Me.lblSum, 0)
            Me.Controls.SetChildIndex(Me.EmployeeControl, 0)
            Me.Controls.SetChildIndex(Me.Label1, 0)
            Me.Controls.SetChildIndex(Me.Belevel1, 0)
            Me.Controls.SetChildIndex(Me.SumBox, 0)
            CType(Me.DescrMemo.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.SumBox.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            Me.ResumeLayout(False)

        End Sub

#End Region

#Region " Data "
        Private ds As New DataAccess.DataSetBill
        Public Shadows Property DataSource() As DataAccess.DataSetBill.PettyCashBill
            Get
                Return CType(MyBase.DataSource, DataAccess.DataSetBill.PettyCashBill)
            End Get
            Set(ByVal Value As DataAccess.DataSetBill.PettyCashBill)
                MyBase.DataSource = Value
            End Set
        End Property

        Protected Overrides Sub DoBinding()
            SumBox.DataBindings.Add("EditValue", DataSource, "PaymentSum")
            DescrMemo.DataBindings.Add("Text", DataSource, "Comment")
            EmployeeControl.EditValue = AppDataLayer.Employees.FindByEmployeeID(DataSource.EmployeeID)
        End Sub


        Public Overrides ReadOnly Property DocumentTitle() As String
            Get
                Return Res.TitlePettyCashBillNew
            End Get
        End Property

        Public Overrides ReadOnly Property Title() As String
            Get
                Return DocumentTitle
            End Get
        End Property

#End Region

#Region " Preferences "
        Protected Overrides Sub LoadPreferences(ByVal pref As Preferences.IPreferencesNode)
            MyBase.LoadPreferences(pref)

            Dim node As IPreferencesNode
            Dim ba As Byte()

            ' EmployeeControl
            node = pref.GetNodeForPath(EmployeeControl.Name)
            ba = node.GetByteArray("Grid", Nothing) : If Not ba Is Nothing Then EmployeeControl.GridView.RestoreLayoutFromStream(New MemoryStream(ba))
            EmployeeControl.PopupWindow.Width = node.GetInt32("PopupWidth", EmployeeControl.PopupWindow.Width)
            EmployeeControl.PopupWindow.Height = node.GetInt32("PopupHeight", EmployeeControl.PopupWindow.Height)
            EmployeeControl.UpdateConfiguration()
        End Sub

        Protected Overrides Sub SavePreferences(ByVal pref As Preferences.IPreferencesNode)
            Dim node As IPreferencesNode
            Dim ms As MemoryStream

            ' EmployeeControl
            node = pref.GetNodeForPath(EmployeeControl.Name)
            ms = New MemoryStream : EmployeeControl.GridView.SaveLayoutToStream(ms) : node.Item("Grid") = ms.ToArray()
            node("PopupWidth") = EmployeeControl.PopupWindow.Width
            node("PopupHeight") = EmployeeControl.PopupWindow.Height

            MyBase.SavePreferences(pref)
        End Sub
#End Region

#Region " Event Handlers "
        Private Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            AddControlToValidate(EmployeeControl, AddressOf EmployeeControl_Validate)
            AddControlToValidate(SumBox, AddressOf SumBox_Validate)
        End Sub
#End Region

#Region " Life-Cycle Routines "
        Protected Overrides Sub DoSaving()
            AppDataLayer.DoInTransaction(AddressOf MakeBill)
            AppOpenTill()
        End Sub

        Protected Overrides Function CreateNewRow() As DataRow
            Dim cl As DataAccess.DataSetBill.PettyCashBill = ds.PettyCashBills.NewPettyCashBill
            cl.OperatorID = Security.CurrentUser.EmployeeID

            If Not Security.CurrentUser.Sys Then
                cl.EmployeeID = Security.CurrentUser.EmployeeID
            End If

            Return cl
        End Function

        Private Sub MakeBill()
            DataSource.CreationTime = Now()
            ds.PettyCashBills.AddPettyCashBill(DataSource)
            AppDataLayer.UpdatePettyCashBill(DataSource.Table)
        End Sub

#End Region

#Region " Data Exchange "
        Private Sub EmployeeControl_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EmployeeControl.EditValueChanged
            If SuspendDataChangedEvents() Then Return

            If EmployeeControl.EditValue Is Nothing Then
                DataSource.SetEmployeeIDNull()
            Else
                DataSource.EmployeeID = CType(EmployeeControl.EditValue, DataSetData.Employee).EmployeeID
            End If
            ValidateEmployee()
        End Sub
#End Region

#Region " Validation "
        Private Sub EmployeeControl_Validate(ByVal sender As Object, ByVal e As CancelEventArgs)
            ValidateEmployee()
        End Sub

        Private Sub SumBox_Validate(ByVal sender As Object, ByVal e As CancelEventArgs)
            ValidateAmount()
        End Sub

        Private Function ValidateEmployee() As Boolean
            If EmployeeControl.EditValue Is Nothing Then
                ErrorProvider.SetError(EmployeeControl, Res.PettyEmployeeRequiredError)
                Return False
            Else
                ErrorProvider.SetError(EmployeeControl, String.Empty)
                Return True
            End If
        End Function

        Private Function ValidateAmount() As Boolean
            Dim val As Double = CDbl(SumBox.EditValue)
            If val <= 0.0 Then
                ErrorProvider.SetError(SumBox, Res.PettyAmountRequiredError)
                Return False
            Else
                ErrorProvider.SetError(SumBox, String.Empty)
                Return True
            End If
        End Function

        Public Overrides Function DataValidate() As Boolean
            If Not MyBase.DataValidate Then Return False

            Dim res As Boolean = True
            If Not ValidateEmployee() Then
                res = False
            End If

            If Not ValidateAmount() Then
                res = False
            End If

            Return res
        End Function
#End Region

    End Class
End Namespace